package com.moxiao.yfjh.module.blind.service.level;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.level.BlindMemberLevelConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.level.BlindMemberLevelDO;
import com.moxiao.yfjh.module.blind.dal.mysql.level.BlindMemberLevelMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员等级 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindMemberLevelServiceImpl implements BlindMemberLevelService {

    @Resource
    private BlindMemberLevelMapper blindMemberLevelMapper;

    @Override
    public Long createMemberLevel(BlindMemberLevelCreateReqVO createReqVO) {
        // 插入
        BlindMemberLevelDO memberLevel = BlindMemberLevelConvert.INSTANCE.convert(createReqVO);
        blindMemberLevelMapper.insert(memberLevel);
        // 返回
        return memberLevel.getId();
    }

    @Override
    public void updateMemberLevel(BlindMemberLevelUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberLevelExists(updateReqVO.getId());
        // 更新
        BlindMemberLevelDO updateObj = BlindMemberLevelConvert.INSTANCE.convert(updateReqVO);
        blindMemberLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberLevel(Long id) {
        // 校验存在
        validateMemberLevelExists(id);
        // 删除
        blindMemberLevelMapper.deleteById(id);
    }

    private void validateMemberLevelExists(Long id) {
        if (blindMemberLevelMapper.selectById(id) == null) {
            throw exception(MEMBER_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public BlindMemberLevelDO getMemberLevel(Long id) {
        return blindMemberLevelMapper.selectById(id);
    }

    @Override
    public List<BlindMemberLevelDO> getMemberLevelList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blindMemberLevelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindMemberLevelDO> getMemberLevelPage(BlindMemberLevelPageReqVO pageReqVO) {
        return blindMemberLevelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlindMemberLevelDO> getMemberLevelList(BlindMemberLevelExportReqVO exportReqVO) {
        return blindMemberLevelMapper.selectList(exportReqVO);
    }

    @Override
    public BlindMemberLevelDO getMemberLevelTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<BlindMemberLevelDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(BlindMemberLevelDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(BlindMemberLevelDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return blindMemberLevelMapper.selectOne(lqw);
    }

    @Override
    public Integer getMemberLevelNo(String levelNo) {
        return blindMemberLevelMapper.getMemberLevelNo(levelNo);
    }

}
